//用来控制并发数

package main

import (
	"fmt"
	nsema "github.com/toolkits/concurrent/semaphore" //查看该源码，其实就是利用了channel来做并发控制
	"time"
)

// init sema
var concurrentNum = 200
var sema = nsema.NewSemaphore(concurrentNum)

func do() {
	// use sema
	for i := 0; i < 10000; i++ {
		sema.Acquire()
		go func(num int) {
			defer sema.Release()
			fmt.Println(num)
			time.Sleep(300 * time.Millisecond)
		}(i)
	}
	for {
		if sema.AvailablePermits() == concurrentNum {
			break
		}
	}
	fmt.Println("done")
}

func main() {
	do()
}

//这种控制并发的方式不太可取，还得加个for循环来同步退出，另外一个实现方式参考channel9.go
